package com.diver.flinkdemo.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author liuC
 * @Date 2023/3/29 0029 14:05
 * @Describe
 */
@Slf4j
public class DatasourceConfigUtil {
    //数据源常量
    private static final String DataSource_MYSQL="dataSource-mysql";
    private static final String DataSource_DORIS="dataSource-doris";
    //配置文件常量
    private static final String Config_MYSQL= "org.jeecg.mapper/mybatis-config-mysql.xml";
    private static final String Config_DORIS= "org.jeecg.mapper/mybatis-config-doris.xml";
    public static SqlSessionFactory mysqlSqlSessionFactory;
    public static SqlSessionFactory dorisSqlSessionFactory;

//    @PostConstruct
    public static void datasourceConfig(){
        SqlSessionFactoryBuilder mysqlBuilder=new SqlSessionFactoryBuilder();
        SqlSessionFactoryBuilder dorisBuilder=new SqlSessionFactoryBuilder();
        //数据源bsm:
        mysqlDatasourceConfig(mysqlBuilder);
        dorisDatasourceConfig(dorisBuilder);
    }

    private static void mysqlDatasourceConfig(SqlSessionFactoryBuilder builder){
        try {
            InputStream in_MYSQL = Resources.getResourceAsStream(Config_MYSQL);
            mysqlSqlSessionFactory = builder.build(in_MYSQL,DataSource_MYSQL);

            /*Configuration configuration = sqlSessionFactory1.getConfiguration();
            Environment environment = configuration.getEnvironment();
            DataSource dataSource = environment.getDataSource();
            TransactionFactory transactionFactory = environment.getTransactionFactory();*/
        } catch (IOException e) {
            log.error("mysql数据源配置出错:", e);
            e.printStackTrace();
        }
    }

    private static void dorisDatasourceConfig(SqlSessionFactoryBuilder builder){
        try {
            InputStream in_DORIS = Resources.getResourceAsStream(Config_DORIS);
            dorisSqlSessionFactory = builder.build(in_DORIS,DataSource_DORIS);

            /*Configuration configuration2 = sqlSessionFactory2.getConfiguration();
            Environment environment2 = configuration2.getEnvironment();
            DataSource dataSource2 = environment2.getDataSource();
            TransactionFactory transactionFactory2 = environment2.getTransactionFactory();*/
        } catch (IOException e) {
            log.error("doris数据源配置出错:", e);
            e.printStackTrace();
        }
    }
}